<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace Plugin\Attractions\AttractionManagement\Repository\Attraction;

use Plugin\Attractions\AttractionManagement\Model\Attraction\Attraction;
use Hyperf\Database\Model\Builder;
use App\Repository\IRepository;
use Hyperf\Collection\Arr;

/**
 * 景区表 Repository类
 */
class AttractionRepository extends IRepository
{
   public function __construct(
        protected readonly Attraction $model
    ) {}

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        
        // id
        if (isset($params['id']) && !empty($params['id'])) {
            $query->where('id', '=', $params['id']);
        }

        // 景区名称
        if (isset($params['name']) && !empty($params['name'])) {
            $query->where('name', 'like', '%'.$params['name'].'%');
        }

        // 景区介绍标题
        if (isset($params['intro_title']) && !empty($params['intro_title'])) {
            $query->where('intro_title', 'like', '%'.$params['intro_title'].'%');
        }

        // 景区介绍
        if (isset($params['intro']) && !empty($params['intro'])) {
            $query->where('intro', '=', $params['intro']);
        }

        // 封面Id
        if (isset($params['cover_id']) && !empty($params['cover_id'])) {
            $query->where('cover_id', '=', $params['cover_id']);
        }

        // 地址
        if (isset($params['address']) && !empty($params['address'])) {
            $query->where('address', 'like', '%'.$params['address'].'%');
        }

        // 联系电话
        if (isset($params['phone']) && !empty($params['phone'])) {
            $query->where('phone', 'like', '%'.$params['phone'].'%');
        }

        // 开门时间
        if (isset($params['open_time']) && !empty($params['open_time'])) {
            $query->where('open_time', 'like', '%'.$params['open_time'].'%');
        }

        // 关门时间
        if (isset($params['close_time']) && !empty($params['close_time'])) {
            $query->where('close_time', 'like', '%'.$params['close_time'].'%');
        }

        // 状态1-正常 2-关闭
        if (isset($params['status']) && !empty($params['status'])) {
            $query->where('status', '=', $params['status']);
        }

        // 
        if (isset($params['created_at']) && !empty($params['created_at']) && is_array($params['created_at']) && count($params['created_at']) == 2) {
            $query->whereBetween(
                'created_at',
                [ $params['created_at'][0], $params['created_at'][1] ]
            );
        }

        // 
        if (isset($params['updated_at']) && !empty($params['updated_at']) && is_array($params['updated_at']) && count($params['updated_at']) == 2) {
            $query->whereBetween(
                'updated_at',
                [ $params['updated_at'][0], $params['updated_at'][1] ]
            );
        }

        return $query;
    }
}